package co.fitstart.api.handler.impl;

import java.util.Arrays;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import com.demo2do.core.support.Result;
import com.demo2do.core.utils.JsonUtils;
import com.demo2do.core.wechat.corp.handler.CorpMessageHandler;
import com.demo2do.core.wechat.corp.message.CorpMessage;
import com.demo2do.core.wechat.corp.message.CorpMessageStruct;
import com.demo2do.core.wechat.corp.message.CorpMessageType;
import com.demo2do.core.wechat.corp.message.CorpMessageStruct.CorpMessageStructBuilder;

import co.fitstart.api.entity.MessageMapper;
import co.fitstart.api.entity.Target;
import co.fitstart.api.handler.TencentHandler;

/**
 * 
 * @author haiyan_xiao
 *
 */
@Component("tencentHandler")
public class TencentHandlerImpl implements TencentHandler {

    private static final Logger logger = LoggerFactory.getLogger(TencentHandler.class);

    @Autowired
    private CorpMessageHandler corpMessageHandler;

    @Value("#{ctx['enterprise.agentid']}")
    private int agentid;

    /*
     * (non-Javadoc)
     * @see co.fitstart.api.handler.TencentHandler#sendMessage(java.lang.String, java.lang.String[])
     */
    public Result sendMessage(String message, String... mobiles) {
        MessageMapper messageMapper = this.generateMessageMapper(message, mobiles);
        
        CorpMessageStruct corpMessageStruct = this.generateCorpMessageStruct(messageMapper);
        
        logger.trace("#sendMessage with CorpMessageStruct["+ corpMessageStruct.asJsonString() +"]");
        
        Result result = new Result();
        try {
            
            result = corpMessageHandler.send(corpMessageStruct);
            
            if(result.isValid()) {
                
                result.success().message("发送消息成功");
                
                logger.trace("#sendMessage: send corpMessage success!");
                
            } else {
                
                logger.error("#sendMessage: send corpMessage fail with Result [" + JsonUtils.toJsonString(result) + "]");
                
                result.fail().message("发送消息失败");
            }
            
            
        } catch (Exception e) {
            logger.error("#sendMessage: occur expection ["+ e + "]");
            result.fail().message("发送消息失败");
        }
        
        return result;
    }

    /**
     * 
     * @param message
     * @param mobiles
     * @return
     */
    private MessageMapper generateMessageMapper(String message,  String... mobiles) {
        
        List<String> targetIds = Arrays.asList(mobiles);
        
        MessageMapper messageMapper = new MessageMapper(message, targetIds, agentid, Target.USER, CorpMessageType.TEXT);
        
        return messageMapper;
    }

    /**
     * 
     * @param messageMapper
     * @return
     */
    private CorpMessageStruct generateCorpMessageStruct(MessageMapper messageMapper) {
        
        CorpMessageStructBuilder builder = messageMapper.initBuilder();
        
        CorpMessage corpMessage = messageMapper.initCorpMessgae();
        
        CorpMessageStruct corpMessageStruct = builder.build(corpMessage);
        
        return corpMessageStruct;
    }
}
